<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				instCacheKey:"hr-u-tempInstAdjustMulti-inst-cache",
				tempInstTemplateCache:'tempInstTemplate-data-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			verifyLink(){
				let _that = this
				if(!_that.$refs.autoform.formdata.instShortId){
					let title = _that.$refs.autoform.getEditorTitle("title","instShortId")
					_that.om.toast('请先选择'+title)
					return false
				}else{
					return true
				}
			},
			verifyPlanPosAndEmp(){
				let _that = this
				// let editor1 = _that.$refs.autoform.getEditor("topinfo","zposId")
				let editor2 = _that.$refs.autoform.getEditor("title","instShortId")
				let editor3 = _that.$refs.autoform.getEditor("title","orgShortId")
				if(!_that.$refs.autoform.formdata.orgShortId || !_that.$refs.autoform.formdata.instShortId){
					_that.om.toast("请先选择"+","+editor2.title+"和"+editor2.title)
					return false
				}else{
					return true
				}
			},
			formSubmit(){
				let _that = this
				_that.om.formSave("tempInstAdjustMulti/addTempInstAdjustMulti",_that.formdata,_that)
			},
			getWorkParam(){
				let _that = this
				return {
					posId:_that.$refs.autoform.formdata.zposId,
					instId:_that.$refs.autoform.formdata.instShortId,
					formCode:_that.busCode
				}
			},
			initInstData(orgId){
				let _that  = this
				_that.$refs.autoform.changeEditor("title","instShortId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							orgId:orgId,
							// zposId: _that.$refs.autoform.formdata.zposId
						}),
						url:'tempInstAdjust/findNotAllAndSelf',
						namefield: 'text',
						initload: true,
						method:"post",
						cachename: _that.instCacheKey,
						placeholder: '请输入机构名称'
					}
				})
			},
			onSelectOrg(item){
				let _that = this
				_that.initInstData(item.id)
			},
			verifyZpos(){
			},
			onSelectZpos(item){
				let _that  = this
				// _that.getOrgList(item.id)
			},
			getOrgList(){
				let _that  = this
				_that.om.request(
					"tempInstAdjust/getOrgList",
					{
						zuserId: _that.userId
					},
					"post"
				).then(res=>{
					_that.$refs.autoform.changeEditor("title","orgShortId","data",configSelectData(res,"id","shortname"))
					if(!_that.$refs.autoform.formdata.orgShortId){
						_that.$refs.autoform.initSelectVal("title","orgShortId")
					}else{
						_that.$refs.autoform.initSelectIndex('title',"orgShortId")
					}
				})
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"wxPosBusiAuth/defaultTableMaker",
						param: {
							code: options.code
						},
						method: "post"
					}
				]
				if(fileId){
					ajaxArr.push({
						url:'tempInstAdjustMulti/getInfo',
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					var defaultPosId = res[1].value.id || '';
					var defaultPosName = res[1].value.posName || '';
					var defaultInstId = res[1].value.instId || '';
					var defaultInstName = res[1].value.instName || '';
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeConfigAttr("planPosAndEmpInfo", "showButton", true,mainData)
					changeConfigAttr("planPosAndEmpInfo", "buttonText", "调岗模板",mainData)
					changeConfigAttr("planPosAndEmpInfo", "verifyLink", "verifyLink",mainData)
					if(_that.subEditorObj["planPosAndEmpInfo"]){
						changeEditor("planPosAndEmpInfo","objStr",{
							getWorkParam:"getWorkParam",
							titleTemplate:"姓名:<%=userName || '未填写'%>",
							subtitle1Template:"事由:<%=reasonName|| '未填写'%>",
							subtitle2Template:"岗位名称:<%=posName || '未填写'%>"
						},null,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
							for(let index in res[2].value.tempInstAdjustMulti){
								if(fieldArr.includes(index)){
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.tempInstAdjustMulti[index])
									_that.$set(_that.formdata,index,res[2].value.tempInstAdjustMulti[index])
								}
							}
						let dataList = _that._.clone(res[2].value.tempInstAdjustMulti_datas)
						if(_that.subEditorObj["planPosAndEmpInfo"]){
							_that.subEditorObj["planPosAndEmpInfo"].workList = dataList
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"objStr",JSON.stringify(dataList))
							_that.$set(_that.formdata,"objStr",JSON.stringify(dataList))
							delete res[2].value.dataList
						}
						if(_that.$refs.autoform.formdata.instShortId){
							changeConfigAttr("planPosAndEmpInfo", "buttonLink","/pages/form/hr-u-tempInstAdjustMulti/tempInstTemplate?instId="+_that.$refs.autoform.formdata.instShortId,mainData)
						}
						if(_that.$refs.autoform.formdata.orgShortId){
							changeEditor("title","instShortId",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({
										orgId:_that.$refs.autoform.formdata.orgShortId,
										// zposId: _that.$refs.autoform.formdata.zposId
									}),
									namefield: 'text',
									url:'tempInstAdjust/findNotAllAndSelf',
									method:'POST',
									initload: true,
									cachename: _that.instCacheKey,
									placeholder: '请输入机构名称'
								}
							},null,mainData)
						}
					}
					_that.$nextTick(function(){
						_that.getOrgList()
						// if(!_that.$refs.autoform.formdata.zposId){
						// 	_that.$refs.autoform.initSelectVal("topinfo","zposId")
						// }else{
						// 	_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
						// }
						
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.formdata.zposId = defaultPosId
							_that.$refs.autoform.formdata.zposName = defaultPosName
							_that.$refs.autoform.formdata.zinstId = defaultInstId
							_that.$refs.autoform.formdata.zinstName = defaultInstName
							_that.$nextTick(function() {
								_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
							})
						}else{
							_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
						}
						
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
		//#ifdef APP-PLUS
		let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj){
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
					//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fieldId in _that.subEditorObj){
					var editor = _that.subEditorObj[fieldId]
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.instCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instShortId = d.id
							formdata.instShortName = d.text
							_that.om.request(
								"tinstitution/getInstInfo",
								{
									instId:d.id
								}
							).then(res=>{
								_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
								_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
							})
							_that.$refs.autoform.changeConfigAttr("planPosAndEmpInfo", "buttonLink","/pages/form/hr-u-tempInstAdjustMulti/tempInstTemplate?instId="+d.id)
							uni.removeStorage({
								key:_that.instCacheKey
							})
						}
					}
				})
				try {
					let value = uni.getStorageSync(_that.tempInstTemplateCache);
					if (value) {
						if(_that.subEditorObj["planPosAndEmpInfo"]){
							_that.subEditorObj["planPosAndEmpInfo"].workList = value
							uni.removeStorageSync(_that.tempInstTemplateCache)
						}
					}
				} catch (e) {
					// error
				}
			}
		}
	}
</script>

<style></style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
